#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function MnuNew()

   local cMnuName := cNewFileName( "menus\MENU", "mnu" )

   if ! File( cMnuName )
      DbCreate( cMnuName, { { "MENUITEM", "C", 60, 0 },;
                            { "ID",       "N",  4, 0 },;
                            { "MESSAGE",  "C", 60, 0 },;
                            { "ACTION",   "C", 60, 0 } } )
   endif

   MnuOpen( cMnuName )

return nil

//----------------------------------------------------------------------------//

function MnuOpen( cMnuName )

   local oIco, oWndMenu, oBar, oLbx, oMenuTree, oItem

   DEFAULT cMnuName := cGetFile( "FiveWin menu (*.mnu) | *.mnu",;
                                 "Select a menu", 1, "menus" )

   TREE oMenuTree BITMAPS "PluSmal", "MinSmal"

      TREEITEM oItem PROMPT "&First"
      TREE
         TREEITEM "&One"
         TREE
            TREEITEM "&One"
            TREEITEM "&Two"
            TREEITEM "&Three"
         ENDTREE
         TREEITEM "&Two"
         TREEITEM "&Three"
      ENDTREE

      TREEITEM "&Second"
      TREE
         TREEITEM "&One"
         TREEITEM "&Two"
         TREEITEM "&Three"
      ENDTREE

      TREEITEM "&Third"
      TREE
         TREEITEM "&One"
         TREEITEM "&Two"
         TREEITEM "&Three"
      ENDTREE
   ENDTREE

   DEFINE ICON oIco RESOURCE "Menu"

   DEFINE WINDOW oWndMenu FROM 1, 1 TO 23, 33 ;
      TITLE "Menu: " + cFileNoExt( cFileNoPath( cMnuName ) ) ;
      ICON oIco

   DEFINE BUTTONBAR oBar _3D SIZE 33, 33 OF oWndMenu

   DEFINE BUTTON RESOURCE "New"  OF oBar ;
      TOOLTIP "New Item" ;
      MESSAGE "Add a new menuitem" ;
      ACTION AddMenuItem( oMenuTree, oLbx )

   DEFINE BUTTON RESOURCE "Edit" OF oBar ;
      TOOLTIP "Edit Item" ;
      MESSAGE "Edit this menuitem"

   DEFINE BUTTON RESOURCE "Del"  OF oBar ;
      TOOLTIP "Del Item" ;
      MESSAGE "Del this menuitem"

   DEFINE BUTTON RESOURCE "Exec" GROUP OF oBar ;
      TOOLTIP "Test" ;
      MESSAGE "Test this menu" ;
      ACTION TestMenu( oMenuTree )

   DEFINE BUTTON RESOURCE "RC"   OF oBar ;
      MESSAGE "Generates a Windows menu RC"

   DEFINE BUTTON RESOURCE "PRG"  OF oBar ;
      MESSAGE "Generates FiveWin menu source code"

   DEFINE BUTTON RESOURCE "Font" GROUP OF oBar ;
      ACTION oLbx:SelFont() MESSAGE "Select a new font or text color"

   DEFINE BUTTON RESOURCE "Clr" OF oBar ;
      ACTION oLbx:SelColor() MESSAGE "Select a new background color"

   @ 0, 0 LISTBOX oLbx FIELDS "" ;
      HEADERS "PullDown Menu Items" ;
      OF oWndMenu ;
      SIZE 400, 400

   oLbx:SetTree( oMenuTree )
   // oLbx:bRClicked = { || ObjInspect( oMenuTree, "oMenuTree" ) }
   oWndMenu:oClient = oLbx

   ACTIVATE WINDOW oWndMenu

return nil

//----------------------------------------------------------------------------//

static function TestMenu( oMenuTree )

   local oWndTest

   DEFINE WINDOW oWndTest TITLE "Testing Menu" ;
      MENU BuildTest( oMenuTree )

   ACTIVATE WINDOW oWndTest

return nil

//----------------------------------------------------------------------------//

static function BuildTest( oMenuTree )

   local oMenu
   local oItem := oMenuTree:oFirst

   MENU oMenu
      while oItem != nil
         MenuAddItem( oItem:cPrompt, "" )
         if oItem:oTree != nil
            BuildTest( oItem:oTree )
         endif
         if oItem:oNext != nil
            if oItem:oNext:nLevel >= oItem:nLevel
               oItem = oItem:oNext
            else
               oItem = nil
            endif
         else
            oItem = nil
         endif
      end
   ENDMENU

return oMenu

//----------------------------------------------------------------------------//

static function AddMenuItem( oMenuTree, oLbx )

   local oDlg, oTree
   local nType := 1

   DEFINE DIALOG oDlg RESOURCE "NewMenuItem"

   REDEFINE RADIO nType ID 110, 120, 130 OF oDlg

   ACTIVATE DIALOG oDlg CENTERED

   if oDlg:nResult == 1
      do case
         case nType == 1
              oLbx:Cargo:Add( "&MenuItem" )
              oMenuTree:Draw()
              oLbx:Refresh()

         case nType == 2
              TREE oTree
                 TREEITEM "&MenuItem"
              ENDTREE
              oLbx:Cargo:oTree = oTree
              oTree:oFirst:oPrev = oLbx:Cargo
              oTree:oFirst:oNext = oLbx:Cargo:oNext
              oTree:oFirst:nLevel = oLbx:Cargo:nLevel + 1
              oTree:oLast = oTree:oFirst
              oMenuTree:Draw()
              oLbx:Refresh()
      endcase
   endif

return nil

//----------------------------------------------------------------------------//